package com.game.business.service;

import com.easemob.im.ApiException;
import com.easemob.im.ApiResponse;
import com.easemob.im.api.GroupApi;
import com.easemob.im.api.MessageApi;
import com.easemob.im.api.UserApi;
import com.easemob.im.api.model.*;
import com.game.business.domain.TGroup;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class HxSevice {

    private UserApi userApi = new UserApi();
    private GroupApi groupApi = new GroupApi();

    private MessageApi messageApi = new MessageApi();

    public void createUser(String  userName) {
        List<EMCreateUser> emCreateUserList = new ArrayList<>();
        EMCreateUser createUser = new EMCreateUser();
        createUser.setUsername(userName);
        createUser.setPassword("123");
        emCreateUserList.add(createUser);
        try {
            EMCreateUsersResult result = userApi.createUsers(emCreateUserList);
            System.out.println("注册账号成功："+result);
        } catch (Exception e) {
            e.getMessage();
            System.out.println("注册账号出现异常："+e.getMessage());
        }
    }
    /**
     * 创建群聊
     */
    public String createGroupAndMembers(String groupName,String description, String ownerUser,List<String> members) {
        String groupid="";
        try {
            // 创建群聊需要的参数
            // 示例实现（具体参数根据环信SDK文档调整）:
            EMCreateGroup options = new EMCreateGroup();
            options.setGroupname(groupName);
            options.setDescription(description);
            options.setOwner(ownerUser);
            options.setMembers(new ArrayList<>());
            options.setMaxusers(200); // 最大成员数
            options.setPublic(true); // 是否公开群
            EMCreateGroupResult result = groupApi.createGroup(options);
            EMCreateGroupResource data = result.getData();
            groupid = data.getGroupid();
            System.out.println("群聊创建成功，群ID: " + data.toString());
            return groupid;
            // System.out.println("群聊创建成功，群ID: " + result.getGroupId());
        } catch (Exception e) {
            e.printStackTrace();
            // 处理创建群聊异常
            System.err.println("创建群聊失败: " + e.getMessage());
            return groupid;
        }
    }


    public String createGroup(String groupName,String description, String ownerUser){
        String groupid="";
        try {
            // 创建群聊需要的参数
            // 示例实现（具体参数根据环信SDK文档调整）:
            EMCreateGroup options = new EMCreateGroup();
            options.setGroupname(groupName);
            options.setDescription(description);
            options.setOwner(ownerUser);
            options.setMembers(new ArrayList<>());
            options.setMaxusers(200); // 最大成员数
            options.setPublic(true); // 是否公开群
            EMCreateGroupResult result = groupApi.createGroup(options);
            EMCreateGroupResource data = result.getData();
             groupid = data.getGroupid();
            System.out.println("群聊创建成功，群ID: " + data.toString());
            return groupid;
        } catch (Exception e) {
            e.printStackTrace();
            // 处理创建群聊异常
            System.err.println("创建群聊失败: " + e.getMessage());
            return groupid;
        }
    }

    public void modifyGroup(String groupId, TGroup group) {
        EMModifyGroup  modifyGroup = new EMModifyGroup();
        modifyGroup.setDescription(group.getDescription());
        modifyGroup.setGroupname(group.getGroupName());
        try {
            EMModifyGroupResult result = groupApi.modifyGroup(groupId, modifyGroup);
            System.out.println("群聊修改成功");
        } catch (ApiException e) {
            System.err.println("修改群聊失败: " + e.getMessage());
        }
    }


    /**
     * 向群聊中添加多个成员
     */
    /**
     * 向群聊中添加多个成员
     */
    public void addMembersToGroup(String groupId, List<String> members) {
        try {
            //使用GroupApi的addMultipleUserToGroup方法向群聊中添加成员
            //需要创建EMAddMultipleUserToGroup对象并设置成员列表
            EMAddMultipleUserToGroup addUser = new EMAddMultipleUserToGroup();
            addUser.setUsernames(members);
            EMAddMultipleUserToGroupResult result = groupApi.addMultipleUserToGroup(groupId, addUser);
            System.out.println("成功向群聊添加成员");
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("向群聊添加成员失败: " + e.getMessage());
        }
    }

    /**
     * 向群聊中添加一个成员
     */
    public void addMembersToGroup(String groupId, String  userName) {
        try {
            EMAddUserToGroupResult result = groupApi.addUserToGroup(groupId, userName);
            System.out.println("成功向群聊添加成员");
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("向群聊添加成员失败: " + e.getMessage());
        }
    }

    /**
     * 群聊中删除多个成员
     */
    public void removeUsersFromGroup(String groupId, List<String> members) {
        try {
            // 使用GroupApi的removeMultipleUserFromGroup方法从群聊中删除成员
            groupApi.removeUsersToGroup(groupId, members);
            System.out.println("成功从群聊删除成员");
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("从群聊删除成员失败: " + e.getMessage());
        }
    }
    /**
     * 群聊中删除一个成员
     */
    public void removeUserFromGroup(String groupId, String username){
        try {
            // 使用GroupApi的removeMultipleUserFromGroup方法从群聊中删除成员
            groupApi.removeUserToGroup(groupId, username);
            System.out.println("成功从群聊删除成员");
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("从群聊删除成员失败: " + e.getMessage());
        }
    }
    /**
     * 发送消息接口
     */
    public void sendMessagesToGroup(String content,String  fromuserName,String type,String  groupId) {
        try {
            EMCreateMessage emCreateMessage = new EMCreateMessage();
            emCreateMessage.setFrom(fromuserName);
            List<String> toList= new ArrayList<>();
            toList.add(groupId);
            emCreateMessage.setTo(toList);
            emCreateMessage.setType( type);
            EMMessageContent messageBody = new EMMessageContent();
            messageBody.setMsg( content);
            emCreateMessage.setBody(messageBody);
            EMSendMessageResult emSendMessageResult = messageApi.sendMessagesToGroup(emCreateMessage);
            Object data = emSendMessageResult.getData();
            System.out.println("发送消息成功，消息ID: " + data);
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("发送消息失败: " + e.getMessage());
        }
    }


}
